package com.my12306.train.controller;

import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.model.entity.CarriageEntity;
import com.my12306.train.service.CarriageService;
import com.my12306.user.model.vo.CarriageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 车厢表
 *
 * @author ËÕÄÁÏ¦
 */
@RestController
@RequestMapping("/carriage")
public class CarriageController {
    @Autowired
    private CarriageService carriageService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public ResponseData list(CarriageVO carriageVO, @RequestParam(defaultValue = "1") long page, @RequestParam(defaultValue = "5") long size) {
        PageUtils page1 = carriageService.find(carriageVO, page, size);

        return ResponseData.ok().put("page", page1);
    }

    /**
     * 列表
     */
    @PostMapping("tc/list")
    public ResponseData list(@RequestBody PageVo pageVo) {
        PageUtils page = carriageService.queryCarriagePage(pageVo);
        return ResponseData.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{carriageId}")
    public ResponseData info(@PathVariable("carriageId") Long carriageId) {
        CarriageEntity carriage = carriageService.getById(carriageId);

        return ResponseData.ok().put("carriage", carriage);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public ResponseData save(@RequestBody CarriageEntity carriage) {
        carriageService.saveCarriage(carriage);
        return ResponseData.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public ResponseData update(@RequestBody CarriageEntity carriage) {
        carriageService.updateCarriageById(carriage);
        return ResponseData.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public ResponseData delete(@RequestBody List<Long> carriageIds) {
        System.out.println(carriageIds);
        carriageService.deleteCarriageBatchByIds(carriageIds);
        return ResponseData.ok();
    }

    /**
     * 查询未编组的车厢
     */
    @GetMapping("/queryCarriageNoMar")
    public ResponseData queryCarriageNoMar() {
        List<CarriageEntity> carriages = carriageService.queryCarriageNoMar();
        return ResponseData.ok().put("carriages", carriages);
    }
}
